﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TESTCommons.ConvertTpye;
using TESTCommons.JsonHelper;
using TESTCommons.LinqUtil;
using TestDBData.EFContext;
using TestModel.SysManage;

namespace TestService.SysManage
{
    public partial class Sys_UserService : ISys_UserService
    {
        #region 参数
        private readonly IRepository<sys_User> _userRepository;
        #endregion

        #region 构造函数
        public Sys_UserService(IRepository<sys_User> userRepository)
        {
            _userRepository = userRepository;
        }
        #endregion

        /// <summary>
        /// 注册
        /// </summary>
        public void SignIn(sys_User model)
        {
            _userRepository.Insert(model);
        }
        /// <summary>
        /// 登陆
        /// </summary>
        public sys_User Login(sys_User model)
        {
            //var user = _userRepository.Table.Where(it => it.userLoginName==model.userLoginName).FirstOrDefault();
            var user = _userRepository.Table.FirstOrDefault(it => it.userLoginName == model.userLoginName);
            return user;
        }
        /// <summary>
        /// 查询model
        /// </summary>
        public sys_User GetModel(Guid gid)
        {
            return _userRepository.GetById(gid);
        }
        /// <summary>
        /// 修改
        /// </summary>
        public void UpdateUser(sys_User model)
        {
            _userRepository.Update(model);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        public void DeleteData(string ids)
        {
            _userRepository.Commit(() =>
            {
                ids.DeserializeObject<List<Guid>>().ForEach(it =>
                {
                    var model = _userRepository.GetById(it.ToGuid());
                    if (model != null)
                    {
                        _userRepository.Delete(model);
                    }
                });
            });
        }
        /// <summary>
        /// 查询数据
        /// </summary>
        public PagedList<sys_User> QueryUser(Hashtable query, int pageIndex = 0, int pageSize = int.MaxValue)
        {
            var expression = LinqExtensions.True<sys_User>();
            if (!string.IsNullOrEmpty(query["userLoginName"].ToStr()))
            {
                expression = expression.And(t => t.userLoginName == query["userLoginName"].ToStr());
            }
            if (!string.IsNullOrEmpty(query["userEmail"].ToStr()))
            {
                expression = expression.And(t => t.userEmail == query["userEmail"].ToStr());
            }
            if (!string.IsNullOrEmpty(query["userName"].ToStr()))
            {
                expression = expression.And(t => t.userName == query["userName"].ToStr());
            }
            //if (!string.IsNullOrEmpty(query["isDelete"].ToString()))
            //{
               
            //    expression = expression.And(t => t.isDelete == false);
            //    expression = expression.And(t => t.isDelete == true);
            //}
            //if (fromUtc.HasValue)
            //    expression = expression.And(t => t.createTime >= fromUtc);
            //if (toUtc.HasValue)
            //    expression = expression.And(t => t.createTime <= fromUtc);
            var user = _userRepository.Table.Where(expression.Compile()).ToList();
            return new PagedList<sys_User>(user, pageIndex, pageSize);
        }
    }
}
